package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.entity.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author kcs
 * @date 2021/11/23
 * @TIME:10:44
 */
@RestController
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/checkitem/add.do")
    public Result add(@RequestBody CheckItem checkItem){

        //数据校验
        if(checkItem == null){
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);//参数非法
        }

        //判断该检查项是否已存在
        LambdaQueryWrapper<CheckItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CheckItem::getName,checkItem.getName());
        CheckItem one = checkItemService.getOne(wrapper);

        //如果存在,直接返回结果
        if(one != null){
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL); //检查组已存在
        }

        //不存在,添加
        checkItemService.save(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);//新增检查项成功
    }

    /**
     * 检查项分页查询 -- 条件查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/checkitem/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        //数据校验
        if(queryPageBean == null){
            return null;//参数非法
        }

        //设置分页查询条件
        IPage<CheckItem> iPage = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        LambdaQueryWrapper<CheckItem> wrapper = new LambdaQueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        wrapper.like(queryString != null,CheckItem::getCode,queryString)
                .or()
                .like(queryString != null,CheckItem::getName,queryString);
        checkItemService.page(iPage,wrapper);

        //如果查询页码大于总页数时,设置当前页码为总页数
        if(iPage.getCurrent() > iPage.getPages()){
            iPage.setCurrent(iPage.getPages());
            checkItemService.page(iPage,wrapper);
        }
        return new PageResult(iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @GetMapping("/checkitem/delete.do")
    public Result delete(Integer id){
        //数据校验

        //判断关系表中是否存在该检查项
        boolean flag = checkItemService.findById(id);
        if(flag){
            //在关系表中删除该检查项的所有数据
            checkItemService.delete(id);
        }

        //根据id删除
        checkItemService.removeById(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据ID查询检查项
     * @param id
     * @return
     */
    @GetMapping("/checkitem/findById.do")
    public Result findById(Integer id){

        //数据校验
        if(id == null || id <= 0){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);//参数非法
        }

        //查询并返回结果
        CheckItem checkItem = checkItemService.getById(id);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/checkitem/edit.do")
    public Result edit(@RequestBody CheckItem checkItem){
        //数据校验

        //更新数据库数据
        checkItemService.updateById(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/checkitem/findAll.do")
    public Result findAll(){

        List<CheckItem> all = checkItemService.getAll();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,all);
    }
}
